//
//  SelectMDDMoreScrollView.swift
//  CreateMyCode
//
//  Created by zhibao on 2024/2/26.
//

import UIKit

typealias SelectMDDMoreDicBlock = (_ dic:[String:Any])->()
typealias SelectMDDMoreDealBlock = (_ flag:Bool)->()
@objc enum SelectDifType:Int {
    case edit
    case dianzhen
    case tubiao
}
class SelectMDDMoreScrollView: UIView,LazyScrollViewDelegate {
    var type:SelectDifType = .edit
    var titleContens = [[String:String]]()
    var lazyScrollView:LazyScrollView!
    var selectBlock:SelectMDDMoreDicBlock?
    var dealBlock:SelectMDDMoreDealBlock?
    init(frame: CGRect,type:SelectDifType) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.type = type
        self.getData()
    }
    
    func getData(){
        if type == .edit {
            titleContens = [
                ["title":LocalizedString("模版"),"type":"0","id":"1"]
            ]
        }else if type == .tubiao {
            titleContens = [
                ["title":LocalizedString("图标"),"type":"0","id":"1"]
            ]
        }else if type == .dianzhen{
            titleContens = [
                ["title":LocalizedString("点阵"),"type":"0","id":"1"]
            ]
        }
        self.initView()
    }
    
    fileprivate func initView(){
        lazyScrollView = LazyScrollView(frame: CGRect(x: 0, y: 0, width: self.width, height: self.height - pixw(p: 10)), delegate: self, dataArray: titleContens, withShowItmes: true, withDirection: 2, withSpace: pixw(p: 50))
        self.addSubview(lazyScrollView)
        lazyScrollView.topScrollView.backgroundColor = UIColor.white
        lazyScrollView.topScrollView.setNormalColor("999999", withSelectColor: "2666e4")
        
        let cancelBtn = UIButton(type: .custom)
        cancelBtn.setImage(UIImage(systemName: "xmark"), for: .normal)
        cancelBtn.tintColor = MYColor.color(withHexString: "666666")
        cancelBtn.tag = 100
        cancelBtn.addTarget(self, action: #selector(btnClick(sender: )), for: .touchUpInside)
        self.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { make in
            make.top.equalTo(pixw(p: 5))
            make.left.equalTo(pixw(p: 10))
            make.width.height.equalTo(pixw(p: 40))
        }
        
//        let finishBtn = UIButton(type: .custom)
//        finishBtn.setImage(UIImage(systemName: "paperplane"), for: .normal)
//        finishBtn.tintColor = MYBlue
//        finishBtn.tag = 101
//        finishBtn.addTarget(self, action: #selector(btnClick(sender: )), for: .touchUpInside)
//        self.addSubview(finishBtn)
//        finishBtn.snp.makeConstraints { make in
//            make.top.equalTo(5)
//            make.left.equalTo(SCREEN_WIDTH - 10 - 40)
//            make.width.height.equalTo(40)
//        }
    }
    
    @objc fileprivate func btnClick(sender:UIButton){
        if dealBlock != nil {
            if sender.tag == 100 {
                dealBlock!(false)
            }else{
                dealBlock!(true)
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension SelectMDDMoreScrollView{
    func lazyView(at index: Int32) -> LazyScrollSubView! {
        let subView = SelectTemplateSubView(frame: CGRect(x: 0, y: pixw(p: 60), width: SCREEN_WIDTH, height: self.height - pixw(p: 60)))
        subView.selectBlock = { dic in
            if self.selectBlock != nil {
                self.selectBlock!(dic)
            }
        }
        return subView
    }
}
